
import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams,ActionSheetController } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { StorageService } from './../../../providers/StorageService';
import { UserService } from './../../../providers/UserService';
import {  nailerService } from './../../../providers/nailerService';
import { PIC_URL } from "./../../../providers/Config";
declare var $: any

/**
 * Generated class for the BalancePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-balance',
  templateUrl: 'balance.html',
})
export class BalancePage extends BasePage {
  picrooturl = "";//图片服务地址
  userinfo = {};  //用户信息

  /*控制变量*/
  list:boolean=false;   

  /*各种身份*/
  identify=[{name:"美甲师",checked:"assets/img/Comment/xuanzeshenfen@2x.png",check:true},{name:"美甲店主",checked:"",check:false}];
  identify2=[{name:"美甲师",checked:"assets/img/Comment/xuanzeshenfen@2x.png",check:true}];
  identify3=[{name:"美甲店主",checked:"assets/img/Comment/xuanzeshenfen@2x.png",check:true}];
  identity=" ";   //选择的身份,默认是消费者

  /*获取收益或支出的参数*/
  param={
    user_id:0
  }
  /*接受的数据*/
  IncomPay={};
  total=0 ;//总余额
  todayIncome=0; //当天收益
  todayPay=0; // 当天支出
  allIncome=0; // 总收益
  allPay=0;  //总支出
  //选择的身份类型
  user_type:any;
  id=[];    //确定身份后的选择框里的身份列表
  trap:boolean=false;  //下拉框是否打开

  constructor(injector: Injector, public nailerService: nailerService, public userService: UserService,public stc: StorageService,public actionSheetCtrl: ActionSheetController) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    //this.getToday();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BalancePage');
    //默认身份消费者，默认获取消费者的数据
    //this.getVip();
    //进入页面判断身份
    if(this.userinfo['user_type']=="1,2"){    //美甲师
      this.identity="美甲师";
      this.user_type=1    //身份类型
      this.id=this.identify2;   //身份选择
      this.getNailer();
    }else if(this.userinfo['user_type']=="1,3"){     //美甲店主
      this.identity="美甲店主"
      this.user_type=2   
      this.id=this.identify3;
      this.getToday();
    }else if(this.userinfo['user_type']=="1,2,3"){   //美甲师和美甲店主
      this.identity="美甲师"
      this.user_type=1   
      this.id=this.identify;
      this.getNailer();
    }
  }
  dismissCLI() {
    this.viewCtrl.dismiss(1);
  }
  
  /*控制下拉菜单和箭头的显示隐藏*/
  listShow(){
    if(this.trap==false){
      this.trap=true;
      $(".trap").slideDown(150)
      $(".arrow").addClass("turn");
    }else{
      this.trap=false;
      $(".trap").slideUp(150);
      $(".arrow").removeClass("turn");
    }
 
  }
  /**
   * 在下拉框里选择某个身份，触发相应的操作
   * @param i 身份列表的索引
   */
  chose(i){
    //只能选一个
    this.id.forEach((e)=>{
      e.check=false;
      e.checked="";
    })
    this.id[i].check=true;
    this.id[i].checked="assets/img/Comment/xuanzeshenfen@2x.png"
    this.identity=this.id[i].name;
    //根据选择的身份来获取对应数据
    if(this.id[i].name=="美甲师"){
      this.user_type=1      
      this.getNailer();
    }else if(this.id[i].name=="美甲店主"){
      this.user_type=2
      this.getToday();
    }
    setTimeout(()=>{    
      this.trap=false;
      $(".trap").slideUp(150)
      $(".arrow").removeClass("turn");
    },100)
  }
  /**获取消费者支出账单 */
  // async getVip(){
  //   let param={user_id:this.userinfo['user_id']};
  //   let result=await this.userService.userPayListAPI(param);
  //   console.log(result);
  //   if(result.code==1){
  //     //消费者没有收益和余额
  //       this.total=0;
  //       this.todayIncome=0;
  //       this.allIncome=0;
  //       this.todayPay=result.data.todayPay;
  //       this.allPay=result.data.allPay;
  //   }else{
  //     let toast = this.toastCtrl.create({
  //       message: result.msg,
  //       duration: 1500,
  //      position: 'bottom',
  //     })
  //     toast.present();
  //   }
  // }

  /*获取美甲店收益*/
  async getToday(){
    this.param.user_id=this.userinfo['user_id'];
    console.log(this.param)
    let result = await this.nailerService.getShopIncomeAPI(this.param);
    console.log(result);
    if(result.code==1){
      this.total=result.data.balance;
      this.todayIncome=result.data.todayIncome;
      this.todayPay=result.data.todayPay;
      this.allIncome=result.data.allIncome;
      this.allPay= result.data.allPay;
    }
  }

  //获取美甲师收益
  async getNailer(){
    let param={
      user_id:this.userinfo['user_id']
    }
    let result=await this.nailerService.artistIncomeAPI(param);
    console.log(result);
    if(result.code==0){
      this.IncomPay = result.data;
      this.total=result.data.total_income; //余额
      this.allIncome=result.data.total_income;  //总收益
      this.todayIncome=result.data.today_income;  //当天收益
      this.todayPay=0;
      this.allPay=0;
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }

  //获取总的
  /*账单*/
  gotoBill(){
    let modal = this.modalCtrl.create('BillPage',{identity:this.identity});
    modal.onDidDismiss(data => {

    });
    modal.present();
  }
  
  /**
   * 获取分类账单
   * @param i 查看那种类型的账单   1 当天收益  2 当天支出    3 总收益   4 总支出
   * 
   */
  gotoBill2(i){
    let date={identity:this.identity,kind:i}
    let modal = this.modalCtrl.create('BillPage',{date:date});
    modal.onDidDismiss(data => {

    });
    modal.present();
  }

  //提现
  getMoney(){
    if(this.user_type==undefined){
      this.showAlert("请先选择身份！")
    }else{
        let myModal = this.modalCtrl.create('WithdrawPage',{total:this.total,style:this.user_type},{
      
        });
        myModal.onDidDismiss(data => {
         
        });
        myModal.present();
    }
    
  }
  /**
   * 查看当天收益
   */
  look1(){
    let kind = 1;
    console.log(this.allIncome);
    if(this.todayIncome == 0){
      let toast = this.toastCtrl.create({
        message: '暂无当天收益',
        duration: 1500,
       position: 'bottom',
      })
      toast.present();
    }else{
      this.gotoBill2(kind);
    }
  }
  /**
   * 查看当天支出
   */
  look2(){
    let kind = 2;
    //this.gotoBill2(a1,b1);
    if(this.todayPay == 0){
      let toast = this.toastCtrl.create({
        message: '暂无当天支出',
        duration: 1500,
       position: 'bottom',
      })
      toast.present();
    }else{
      this.gotoBill2(kind);
    }
  }
  /**
   * 查看总收益
   */
  look3(){
    let kind = 3
    //this.gotoBill2(a1,b1);
    if(this.allIncome == 0){
      let toast = this.toastCtrl.create({
        message: '暂无总收益',
        duration: 1500,
       position: 'bottom',
      })
      toast.present();
    }else{
      this.gotoBill2(kind);
    }
  }
  /**
   * 查看总支出
   */
  look4(){
    let kind = 4;
    //this.gotoBill2(a1,b1);
    if(this.allPay == 0){
      let toast = this.toastCtrl.create({
        message: '暂无总支出',
        duration: 1500,
       position: 'bottom',
      })
      toast.present();
    }else{
      this.gotoBill2(kind);
    }
  }
}
